package com.doit.day02

import scala.io.Source

/**
 * order.txt  ==》订单表
 *
 * order_id,user_id,amount
*order011,u001,300  ==》(user_id,order对象)
*order012,u002,200  ==》(user_id,order对象)
*order023,u006,100  ==》(user_id,order对象)
*order056,u007,300  ==》(user_id,order对象)
*order066,u003,500  ==》(user_id,order对象)
*order055,u004,300  ==》(user_id,order对象)
*order021,u005,300  ==》(user_id,order对象)
*order014,u001,100  ==》(user_id,order对象)
*order025,u005,300  ==》(user_id,order对象)
*order046,u007,30   ==》(user_id,order对象)
*order067,u003,340  ==》(user_id,order对象)
*order098,u008,310  ==》(user_id,order对象)
 *
 *user.txt  ==》用户表
 *
 *user_id,name,age,nikeName
*u001,hls,22,fengjie               ==》(user_id,user对象)
*u002,wangwu,31,lisi               ==》(user_id,user对象)
*u003,zhangyanru,22,tananpengyou   ==》(user_id,user对象)
*u004,laocao,26,fengyi             ==》(user_id,user对象)
*u005,mengqi,12,nvmengqi           ==》(user_id,user对象)
*u006,haolei,38,sb                 ==》(user_id,user对象)
*u007,wanghongjing,24,wife         ==》(user_id,user对象)
*u009,wanghongjing,24,wife         ==》(user_id,user对象)
 *
 *返回一个结果：order011  u001   300  hls  22   fengjie
 *
 *思考题：
 *left join    ==》
 *join         ==》
 *
 */

/**
 *  分析逻辑：
 *  1.需要将两张表里面的数据全部读取出来，放在一个集合里面
 *  2.我需要根据两张表的user_id相等==》 做一个join操作   拿着order集合中的每一个元素的user_id 取user集合中获取对应的user对象
 *  3.拿着order对象和user对象，获取到我们想要的所有的数据，封装起来作为集合的元素返回(直接打印出来)
 */
object _03_join语法案例 {
 def main(args: Array[String]): Unit = {
  //1.需要将两张表里面的数据全部读取出来，放在一个集合里面
  val user: List[String] = Source.fromFile("D:\\develop\\ideaWorkSpace\\myself\\Teach\\scalaDemo\\data\\user").getLines().toList
  val order: List[String] = Source.fromFile("D:\\develop\\ideaWorkSpace\\myself\\Teach\\scalaDemo\\data\\order").getLines().toList


  //2.将一行行的数据进行切割操作转换成map集合(user_id,user对象)  (user_id,user对象)

  val userMap: Map[String, (String, String, String, String)] = user.map(line => {
   //u001,hls,22,fengjie
   val arr: Array[String] = line.split(",")
   val user_id: String = arr(0)
   val name: String = arr(1)
   val age: String = arr(2)
   val nickName: String = arr(3)
   (user_id, (user_id, name, age, nickName))
  }).toMap

  //处理订单
  val orderMap: List[(String, (String, String, String))] = order.map(line => {
   //order021,u005,300
   val arr: Array[String] = line.split(",")
   val order_id: String = arr(0)
   val user_id: String = arr(1)
   val price: String = arr(2)
   (user_id, (order_id, user_id, price))
  })

  //对order表获取到每一行数据
  for (elem <- orderMap) {
   //订单中的user_id
   val user_id: String = elem._1
   val order_info: (String, String, String) = elem._2

   //拿着订单里面的user_id 去我们user表中获取对应的user对象

   val flag: Boolean = userMap.contains(user_id)
   if (flag){
    val maybeTuple: Option[(String, String, String, String)] = userMap.get(user_id)
    val tuple: (String, String, String, String) = maybeTuple.get
    //order011  u001   300  hls  22   fengjie
    println((order_info._1, user_id, order_info._3, tuple._2, tuple._3, tuple._4))
   }
/*   val tuple: (String, String, String, String) = userMap.getOrElse(user_id, (null, null, null, null))*/
   //返回最后的结果

  }



  //上述的这种join方式是哪种join方式










 }

}
